/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.access.RequestParameter;
import com.elf.gwt.common.client.model.OrderPreviewModel;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

/**
 *  
 */
public class OrdersPreviewComponent extends EditTableComponent
{
	List<OrderPreviewModel> orderPreviewModels;
	protected OrderPreviewModel selectedOrderPreviewModel;
	
	protected int selectedCustomerId = 0;

	/**
	 * Constructor
	 */
	public OrdersPreviewComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		orderPreviewModels = new ArrayList<OrderPreviewModel>();
		paginate = true;
		editable = false;
		tableType = "orders";
	}
	
	public boolean updateFromHistoryParameters(String[] params)
	{
		selectedCustomerId = 0;
		if (params.length > 2) selectedCustomerId = Integer.parseInt(params[2]);
		return super.updateFromHistoryParameters(params);
	}

	public void additionalRequestParameters()
	{
		if (selectedCustomerId > 0) 
			addRequestParameter(new RequestParameter("customerId", selectedCustomerId));
		super.additionalRequestParameters();
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		super.buildMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		orderPreviewModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		OrderPreviewModel model = new OrderPreviewModel(jsonObject);
		orderPreviewModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = orderPreviewModels.size();
		numberOfTableColumns = 6;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Order Id");
		grid.setText(0, 1, "Customer");
		grid.setText(0, 2, "Total");
		grid.setText(0, 3, "Purchase Date");
		grid.setText(0, 4, "Status");
		grid.setText(0, 5, "Action");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, orderPreviewModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
	}

	public void displayRow(Grid grid, int row, OrderPreviewModel model) {

		CellFormatter cf = grid.getCellFormatter();
		DateTimeFormat df = CommonUtils.dateFormat;
		grid.setText(row, 0, String.valueOf(model.getId()));
		grid.setText(row, 1, model.getCustomerName());
		cf.setHorizontalAlignment(row, 2, HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setHTML(row, 2, CommonUtils.priceDisplay(AdminComponentController.currenciesAdministrationComponent.getCurrencyModel(), model.getTotal()));
		grid.setText(row, 3, df.format(model.getDatePurchased()));
		grid.setText(row, 4, model.getStatus());
		
		InlineLabel detailsLabel = new InlineLabel("details");
		detailsLabel.setStyleName("ComponentLink");
		detailsLabel.addStyleName("index:"+(row-1));
		detailsLabel.addClickHandler(editClickHandler);
		grid.setWidget(row, 5, detailsLabel);
	}

	public List<OrderPreviewModel> getOrderPreviewModels() {
		return orderPreviewModels;
	}

	protected ClickHandler editClickHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
			Widget source = (Widget)event.getSource();
			String stylenames = source.getStyleName();
			int loc = stylenames.indexOf("index:");
			stylenames = stylenames.substring(loc+6);
			loc = stylenames.indexOf(" ");
			if (loc > -1) stylenames = stylenames.substring(0, loc);
			int index = Integer.parseInt(stylenames);
			selectedOrderPreviewModel = orderPreviewModels.get(index);
			String history = "EditOrderComponent--" + selectedGroupId + "--" + selectedOrderPreviewModel.getId() + "--" + selectedCustomerId;
			onSelection(history);
		}
	};
}
